﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;
using System.Web;

namespace iTunesFastForward.Lyrics
{
    static class LyricsWikiREST
    {

        const string GETSONG_URL = "http://lyricwiki.org/api.php?artist={0}&song={1}&fmt=xml";

        public static LyricsDesignation GetSong(string artist, string song)
        {
            string xmlResult = new WebClient().DownloadString(
                string.Format(
                    GETSONG_URL,
                    artist != null ? HttpUtility.UrlEncode(artist) : string.Empty,
                    song != null ? HttpUtility.UrlEncode(song) : string.Empty));

            XDocument xdoc = XDocument.Parse(xmlResult);

            XElement lyricsResult = xdoc.Element("LyricsResult");

            LyricsDesignation res = new LyricsDesignation
            {
                Artist = lyricsResult.Element("artist").Value,
                Song = lyricsResult.Element("song").Value,
                Lyrics = lyricsResult.Element("lyrics").Value
            };

            if (res.Lyrics == "Not found")
                return null;
            else
                return res;

        }
    }
}
